package cn.gx.struts2.action;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * 抽象 action 的超类，专门用于继承
 * @author always
 *
 * @param <T>
 */

@SuppressWarnings("unchecked")
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>,Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7929921984913691177L;
	
	private Logger log=Logger.getLogger(this.getClass());
	protected T model;
	protected Map<String, Object> jsonMap = new HashMap<String, Object>(0);
	protected int pageNum=1;
	protected static  Long loginUserId;
	
	public BaseAction(){
		try {
			ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
			Class<T> modelClass = (Class<T>) type.getActualTypeArguments()[0];
			if(model==null){
				model=modelClass.newInstance();
				log.info("实例化一个模型:"+modelClass.getSimpleName());
			}else{
				log.info("复用一个模型驱动:"+modelClass.getSimpleName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
	}
	
	public void prepare() throws Exception {
		
	}

	public  T getModel(){
		return model;
	}

	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(Map<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public static Long getLoginUserId() {
		return loginUserId;
	}

	public static void setLoginUserId(Long loginUserId) {
		BaseAction.loginUserId = loginUserId;
	}

	
}
